<?php
namespace app\api\controller;
use app\model\AccRefund;
use app\model\Recharge;
use think\facade\Db;
use app\model\Token;
use app\model\OpAccount;
use  app\model\ViewItem;
use app\model\ItemConsume;
class Text{
    //退款回滚
    public function index(){ 
        dump(2);
        die();
        $id = '';
        $r = AccRefund::where('id',$id)->find();
        $recharge = Recharge::where("id",$r['pid'])->find();
        
        // 启动事务
        Db::startTrans();
        try {
            //恢复充值信息
            Db::name('recharge')->where("id",$r['pid'])->inc('currency', $r['r_currency']*1)->update();
            
            //恢复账户信息
            Db::name("op_account")->where("id",$recharge['op_account_id'])->inc('balance', $r['r_currency']*1)->update();
            
            //恢复客余额
            Db::name('customer')->where("id",$recharge['customer_id'])->dec('prepare_money', $r['r_money']*1)->update();
            
            $da['id'] = $id;
            $da['status'] = 1;
            Db::name('acc_refund')->update($da);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            dump($e->getMessage());
        }
        
    }
    
    //测试字节 获取消耗
    function get_ss(){
        set_time_limit(0);
        $wh[] = ['group','=','ziapi'];
        $wh[] = ['acc_id','=','1188'];
        $list = Token::where($wh)->find();
        $t = date("Y-m-d",strtotime("-11 day"));
        $advertiser_id = OpAccount::where('id',$list['acc_id'])->value('name_id');
        $headers = array( 'Access-Token:'.$list['access_token']);
        $url = 'https://ad.oceanengine.com/open_api/2/report/advertiser/get/?advertiser_id='.$advertiser_id.'&start_date='.$t.'&end_date=2020-12-11';
        
        $rs = json_decode(http_get($url,$headers),true);
        // dump($rs);
        if($rs['code'] == 0){
            foreach($rs['data']['list'] as $k=>$v){
                $arr['cost'] = $v['cost'];
                $arr['convert'] = $v['convert'];
                $arr['time'] = strtotime($v['stat_datetime']);
                $arr['advertiser_id'] = $v['advertiser_id'];
                $arr['balance'] = $this->get_zj_balance($headers,$advertiser_id,date("Y-m-d",strtotime($v['stat_datetime'])));
                // $this->add_xiaohao($arr);
                // dump($v);
            }
            // dump($arr);
            
            //$this->add_xiaohao($arr);
        
        }else{
            echo '<br>'.$rs['message'];
           
            // dump($rs['message']);
        }
    }
     //获取字节 昨天结余
     public function get_zj_balance($headers,$advertiser_id,$t){
        $url = 'https://ad.oceanengine.com/open_api/2/advertiser/fund/daily_stat/?advertiser_id='.$advertiser_id.'&start_date='.$t.'&end_date='.$t;
        
        $rs = json_decode(http_get($url,$headers),true);
        // dump($rs['data']);die();
        if(count($rs['data']['list']) > 0 && $rs['code'] == 0){
            return $rs['data']['list'][0]['balance'];
        }else{
            return 0;
        }
    }
    function add_xiaohao($data){
        set_time_limit(0);
        $rs = ViewItem::where('name_id',$data['advertiser_id'])->find();
        $da['items_id'] = $rs['item_id'];
        $da['currency'] = $data['cost'];
        $da['time'] = $data['time'];
        $da['balance'] = $data['balance'];
        $da['operation_id'] = $rs['operate_id'];
        $da['op_account_id'] = $rs['acc_id'];
        if($rs['type'] == 2){
            $da['num'] = $data['convert'];  //表单数量
        }
        $da['is_operate'] = $rs['acc_type'];
       
        $w[] = ['items_id' ,'=',$rs['item_id']];
        $w[] = ['time' ,'=',$data['time']];
        $r = ItemConsume::where($w)->find();
        if(empty($r)){
            try {
                //判断当天是否有切户，有切换账户，获取切换账户消耗
                $c = $this->is_zj_qiehu($rs['item_id'],$data['time'],$rs['acc_id']);
                if($c){
                    $da['currency'] = $data['cost'] - $c;
                }
                
                ItemConsume::create($da);
                echo '<br>账户ID：'.$data['advertiser_id'].' 时间：'.date("Y-m-d",$data['time']).'添加成功<br/>';
            } catch (\Exception $e) {
                echo '<br>账户ID：'.$data['advertiser_id'].' 时间：'.date("Y-m-d",$data['time']).'添加失败，'.$e->getMessage().'<br/>';
               
            }
        }else{
            echo '<br>时间：'.date("Y-m-d",$data['time']).'没有添加数据<br/>';
        } 
    }
    //判断当天是否有切户，有切换账户，获取切换账户消耗
    function is_zj_qiehu($items_id,$time,$op_account_id){
        $w[] = ['items_id' ,'<>',$items_id];
        $w[] = ['time' ,'=',$time];
        $w[] = ['op_account_id' ,'=',$op_account_id];
        $r = ItemConsume::where($w)->find();
        if($r){
            return $r['currency'];
        }else{
            return false;
        }
        
    }
    
    //测试 字节 获取广告投放链接
    function get_ad_url(){
        set_time_limit(0);
        $wh[] = ['group','=','ziapi'];
        $wh[] = ['acc_id','=','1164'];
        $list = Token::where($wh)->find();
        $advertiser_id = OpAccount::where('id',$list['acc_id'])->value('name_id');
        $headers = array( 'Access-Token:'.$list['access_token']);
        $url = 'https://ad.oceanengine.com/open_api/2/ad/get/?advertiser_id='.$advertiser_id;
        
        $rs = json_decode(http_get($url,$headers),true);
       
        if($rs['code'] == 0 ){
            foreach($rs['data']['list'] as $k=>$v){
                $arr[$k]['external_url'] = $v['external_url'];
                $arr[$k]['status'] = $v['status'];
            }
        }
        dump($arr);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
}